package com.wc;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {


    private Text mkey = new Text();
    private final static IntWritable mval = new IntWritable(1);

    /**
     * hello hadoop 1
     * hello hadoop 2
     * TextInputFormat
     *
     * @param key     是每一行字符串自己第一个字节面向源文件的偏移量
     * @param value   每行的字符串
     * @param context 上下文对象
     * @throws IOException
     * @throws InterruptedException
     */
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 切分每行字符串
        String[] arr = value.toString().split(" ");
        System.out.println(Arrays.toString(arr));

        for (String str : arr) {
            // 对引用类型的属性直接赋值，在写出
            mkey.set(str);
            // write 里面有序列化的代码，
            // 下次在写的时候是不会影响到之前写出的值
            context.write(mkey, mval);
        }


    }

}
